/// Setting up the ANES File for presidential coalitions  

use "/Users/joshuazingher/Desktop/Prospectus /anes_cdfdta/anes coalitions .dta"

sort year

rename VCF0704 presvote

rename VCF0703 voted

/// Generating total South respondants by year

by year: egen Southresp = count(VCF0112) if VCF0112==3 

/// Generating total number of South voters by year

by year: egen Southvoted = count(voted) if VCF0112==3 & race==1 & voted==3

/// Generating total number of South non-voters by year

by year: egen SouthNvoted = count(voted) if voted==0 & race==1 & VCF0112==3 | voted==1 & race==1 & VCF0112==3 | voted==2 & race==1 & VCF0112==3


/// Generating total number of Non-South White voters by year

by year: egen NSouthvoted = count(voted) if VCF0112~=3 & voted==3 | VCF0112==3 & race~=1 & voted==3

/// Generating total number of Non-South non-voters by year

by year: egen NSouthNvoted = count(voted) if VCF0112~=3 & voted==0 | VCF0112~=3 & voted==1 | VCF0112~=3 & voted==2 | VCF0112==3 & race~=1 & voted==0 | VCF0112==3 & race~=1 & voted==1 | VCF0112==3 & race~=1 & voted==2 

/// filling in the blanks

by year: egen southvoted = max(Southvoted)
by year: egen southNvoted = max(SouthNvoted)
by year: egen Nsouthvoted = max(NSouthvoted)
by year: egen NsouthNvoted = max(NSouthNvoted)




/// Generating % SouthTO with Mosteller Correction

gen N = 1
gen a = [southvoted * NsouthNvoted] / [southNvoted * Nsouthvoted]


///Generating filler vars

gen aminone =(a-1)
gen AsouthplusTO = (ASW+aTO)
gen asouthTO=(a*ASW*aTO)

gen CNsouthTO = 1/ (2*(aminone)) * (N+(aminone*AsouthplusTO) - sqrt((N+aminone*AsouthplusTO)*(N+aminone*AsouthplusTO) - (4*(aminone*asouthTO))))

///Adjusting TO for the formula

replace CNsouthTO = CNsouthTO/ASW



/// Moving to Partisanship - Generating South Republican Vote total by year

by year: egen southrep = count(VCF0112) if presvote==2 & VCF0112==3 & race==1

/// Generating South Non-Republican Vote by Year

by year: egen southNrep = count(VCF0112) if presvote==1 & VCF0112==3 & race==1

// Generating South Republican Vote total by year

by year: egen Nsouthrep = count(VCF0112) if presvote==2 & VCF0112~=3 | presvote==2 & VCF0112==3 & race==~1 

/// Generating South Non-Republican vote total by year

by year: egen NsouthNrep = count(VCF0112) if presvote==1 & VCF0112~=3 | presvote==1 & VCF0112==3 & race==~1 

/// filling in the blanks

by year: egen SouthRep = max(southrep)
by year: egen SouthNRep = max(southNrep)
by year: egen NSouthRep = max(Nsouthrep)
by year: egen NSouthNRep = max(NsouthNrep)

/// Generation % SouthRepocrat with Mosteller Correction

gen A = (SouthRep*NSouthNRep)/(SouthNRep*NSouthRep)
gen Aminone =(A-1)
gen AsouthplusRep = (ASW+(ARep))
gen AsouthRep=(A*ASW*(ARep))
gen CNsouthRep = 1/(2*(Aminone)) * (N+(Aminone*AsouthplusRep) - sqrt((N+Aminone*AsouthplusRep)*(N+Aminone*AsouthplusRep) - (4*(Aminone*AsouthRep))))

replace CNsouthRep = CNsouthRep/ASW


/// Generating number of ANES respondents by year

by year: egen totalresp = count (VCF0006a)


/// Coalition Contribution

gen southcoalcontrib = (CNsouthTO* CNsouthRep *ASW)/(aTO*(ARep))



